<?php

namespace App\Http\Controllers\Backend;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use App\Models\Category;

class CategoryController extends Controller{

    public function index(Request $request){
        $page = $request->input('page');
        $limit = $request->input('limit');
        $name = $request->input('name','');

        $where = [];
        if(!empty($name)){
            $where[] = ['name','like','%'.$name.'%'];
        }

        $cate_data = Category::select('id','sort','name','brief','created_at')
                    ->where($where)
                    ->orderBy('sort')
                    ->orderBy('updated_at','desc')
                    ->paginate($limit)
                    ->toArray();

        $data = [];

        if($cate_data){
            $data = [
                'code'=>200,
                'msg'=>'success',
                'data'=>$cate_data
            ];
        }else{
            $data = [
                'code'=>100,
                'msg'=>'查询数据异常',
                'data'=>[]
            ];
        }

        return response()->json($data);

    }



    //保存
    public function save(Request $request){
        $name = $request->input('name');
        $brief = $request->input('brief');
        $id = $request->input('id',0);

        $now = date('Y-m-d H:i:s');

        $brief = empty($brief)?'分类简介什么的，你不写我就不客气了！':$brief;

        //修改
        if($id !=0){
            $cate = Category::find($id);
            $cate->updated_at = $now;
        }else{
            $cate = new Category();
            $cate->updated_at = $now;
            $cate->created_at = $now;
            //$cate->sort = 
        } 

        $cate->name = $name;
        $cate->brief = $brief;
        

        $data = [];
        $res = $cate->save();  //添加成功返回true
        $currentId = $cate->id;

        if($id == 0){
            Category::where('id',$currentId)->update(['sort'=>$currentId]);    
        }

        if($res){
            $data = [
                'code'=>200,
                'msg'=>'保存成功'
            ];
        }else{
            $data = [
                'code'=>100,
                'msg'=>'保存失败'
            ];
        }
        return response()->json($data);

    }

    //上下移动
    public function setLevel(Request $request){

        $id = $request->input('id');
        $sort = $request->input('sort');
        $flag = $request->input('flag');

        //dd($request->input());

        if($flag =='up'){//上移

           // \Illuminate\Support\Facades\DB::connection()->enableQueryLog();  // 开启QueryLog

            $cate = Category::where('sort','<',$sort)->orderBy('sort','desc')->first();
            
           // dd(\Illuminate\Support\Facades\DB::getQueryLog());
           
            $upid = $id;
            $upsort = $cate->sort;

            $downid = $cate->id;
            $downsort = $cate->sort + 1;
                   
        }else{//下移
            $cate = Category::where('sort','>',$sort)->orderBy('sort','asc')->first();
            $upid = $cate->id;
            $upsort = $cate->sort - 1;

            $downid = $id;
            $downsort = $cate->sort;

        }

        Category::where('id',$upid)->update(['sort'=>$upsort]);
        Category::where('id',$downid)->update(['sort'=>$downsort]);

        if($cate){
            $data = [
                'code'=>200,
                'msg'=>'保存成功'
            ];
        }else{
            $data = [
                'code'=>100,
                'msg'=>'保存失败'
            ];
        }
        return response()->json($data);


    }

    //分类id name的数组
    public function getCate(){
        $cate_arr = Category::select('id','name')->get()->toArray(); 
        if($cate_arr){
            $data = [
                'code'=>200,
                'msg'=>'success',
                'data'=>$cate_arr
            ];
        }else{
            $data = [
                'code'=>100,
                'msg'=>'查询数据异常',
                'data'=>[]
            ];
        }

        return response()->json($data);

    }




}